package com.xxb.web.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.xxb.annotation.Token;
import com.xxb.constant.ResponseCode;
import com.xxb.constant.ResultCode;
import com.xxb.model.entity.SysUser;
import com.xxb.model.service.ISysUserService;
import com.xxb.utils.DtoUtil;
import com.xxb.utils.JwtUtil;
import com.xxb.web.dto.ResultJson;
import com.xxb.web.dto.SysUserDto;
import com.xxb.web.dto.SysUserLoginDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.xml.ws.Response;


/**
 * @author 陈亮
 * @since 2018-07-07
 */
@Api(tags = "sys_user - Controller")
@RestController
@RequestMapping("/sysUser")
@Slf4j
public class SysUserController {

    @Autowired
    private ISysUserService isys_userService;

    @ApiOperation(value = "添加 - sys_user")
    @PostMapping(value = "insert")
    public ResultJson insert(@RequestBody SysUserDto model) {
        SysUser entity = DtoUtil.convertObject(model, SysUser.class);
        boolean flag = isys_userService.insert(entity);
        if (flag) {
            return ResultJson.getInstance().setCode(ResponseCode.请求成功).setData(null);
        } else {
            return ResultJson.getInstance().setCode(ResponseCode.请求失败).setData(null);
        }
    }

    @ApiOperation(value = "修改 - sys_user")
    @PutMapping(value = "up/{id}")
    public ResultJson update(@RequestBody SysUserDto model) {
        SysUser entity = DtoUtil.convertObject(model, SysUser.class);
        boolean flag = isys_userService.update(entity, null);
        if (flag) {
            return ResultJson.getInstance().setCode(ResponseCode.请求成功).setData(null);
        } else {
            return ResultJson.getInstance().setCode(ResponseCode.请求失败).setData(null);
        }
    }

    @Token
    @ApiOperation(value = "删除 - sys_user")
    @DeleteMapping(value = "delete/{id}")
    public ResultJson delete(@RequestHeader String token, @PathVariable Integer id) {
        boolean flag = isys_userService.deleteById(id);
        if (flag) {
            return ResultJson.getInstance().setCode(ResponseCode.请求成功).setData(null);
        } else {
            return ResultJson.getInstance().setCode(ResponseCode.请求失败).setData(null);
        }
    }

    @Token
    @ApiOperation(value = "查询所有 - sys_user")
    @GetMapping(value = "query")
    public ResultJson<Page<SysUser>> query(@RequestHeader String token) {
        Page<SysUser> page = new Page<SysUser>();
        Page<SysUser> list = page.setRecords(isys_userService.selectList(null));
        return ResultJson.getInstance().setCode(ResponseCode.请求成功).setData(list);
    }

    @ApiOperation(value = "登录 - sys_user")
    @PostMapping(value = "login")
    public ResultJson<SysUserLoginDto> login(@RequestBody SysUserDto model) {
        SysUser entity = DtoUtil.convertObject(model, SysUser.class);
        entity = isys_userService.login(entity);
        if (entity != null) {
            String token = JwtUtil.createJWT(entity.getId().toString(), entity.getName());
            SysUserLoginDto userInfo = new SysUserLoginDto();
            userInfo.setSysUser(entity);
            userInfo.setToken(token);
            return ResultJson.getInstance().setCode(ResponseCode.登录成功).setData(userInfo);
        } else {
            return ResultJson.getInstance().setCode(ResponseCode.登录失败).setData(null);
        }
    }


}

